package com.abkj.platform.tail.interceptor;

import com.abkj.platform.tail.domain.JcUser;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class UserAuthInterceptor
  implements HandlerInterceptor
{
  public boolean preHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2)
    throws Exception
  {
    JcUser user = (JcUser)request.getSession().getAttribute("ABKJ-PT-ACCOUNT");
    if (user == null)
      return login(request, arg1);
    return true;
  }

  public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
    throws Exception
  {
  }

  public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception
  {
  }

  private boolean login(HttpServletRequest request, HttpServletResponse response) throws IOException
  {
    PrintWriter writer = response.getWriter();
    response.setContentType("text/html;charset=UTF-8");
    writer.write("<script  type=\"text/javascript\">");
    writer.write("\twindow.parent.location.href=\"" + request.getContextPath() + "/login\"");
    writer.write("</script>");
    writer.flush();
    writer.close();
    return false;
  }
}